package com.unittec.drainage.provider.scope;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.unittec.drainage.sdk.root.enums.LogLevelE;

/**
 * @author wuxinwei
 */
public class GlobalScope {

    /**
     * 各个业务服务的日志级别为ERROR，通过SDK可以配置应用所属服务的日志级别，日志服务会同步刷新各个服务的日志级别
     */
    private static final LogLevelE logLevel = LogLevelE.ERROR;

    private static final Map<String, LogLevelE> logLevelMap = new ConcurrentHashMap<>();

    private static String serviceName = null;

    private static final Map<String, String> tokenMap = new ConcurrentHashMap<>();

    public static void setServiceName(String name) {
        GlobalScope.serviceName = name;
    }

    public static String getServiceName() {
        return GlobalScope.serviceName;
    }

    public static Boolean checkLogLevelIsEmpty(String appId) {
        LogLevelE logLevel = logLevelMap.get(appId);
        return logLevel == null;
    }

    public static LogLevelE getLogLevel(String appId) {
        LogLevelE logLevel = logLevelMap.get(appId);
        if (null == logLevel) {
            logLevel = GlobalScope.logLevel;
        }

        return logLevel;
    }

    public static void setLogLevel(String appId, LogLevelE loglevel) {
        logLevelMap.put(appId, loglevel);
    }

    public static void setToken(String appId, String token) {
        tokenMap.put(appId, token);
    }

    public static String getToken(String appId) {
        if (null == appId) {
            return "";
        }
        String token = tokenMap.get(appId);
        return (token == null) ? "" : token;
    }
}
